Ext.require([
    'Ext.form.field.ComboBox',
    'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager',
    'Ext.toolbar.Paging',
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.state.*'
]);


Ext.onReady(function() {
	var billUser = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["userName", "id"],
	        proxy:{
	            type:'ajax',
	            url:jypath+'/marketing/haveReceiveRefund/findUserByMerID',
	            reader:{
                    type: 'json', 
                    root: 'root'  //数据
               }
	        },
	        autoLoad:true
    });
	
	var form = new Ext.form.FormPanel({
		title : "更换信息",
		region : 'north',
		frame : true,
		items:[
		       {
		    	   fieldLabel: "原票据号码",
		    	   xtype:'textfield',
		    	   id:'billCode',
		    	   value:billCode,
		    	   readOnly:true
		       }, {
		    	   xtype:"textfield",					
		    	   fieldLabel:"新票据号码",
		    	   id:"newBillCode",
		       }, {  
		            xtype:'combobox',  
		            id:'billUserId',  
		            store: billUser,
		            displayField: "userName",           
		            valueField: "id",  
		            fieldLabel:'更换人员',  
		            queryMode:'local',
		            typeAhead: true
		       }, {
		    	   xtype:'datefield',
		    	   format:'Y-m-d',
		    	   fieldLabel:'更换日期',
		    	   id:'billDate'
		       }, {
		    	   xtype:'textarea',
		    	   fieldLabel:'备注',
		    	   width:500,
			       id:'billRemark'	   
		       }
		 ],
		 dockedItems: [{
	            xtype: 'toolbar',
	            dock: 'bottom',
	            ui: 'footer',
	            layout: {
	                pack: 'left'
	            },
	            items: [{
	                minWidth: 80,
	                text: '保存',
	                id:'save_button',
	                handler:changeBill
	            },{
	                minWidth: 80,
	                text: '返回',
	                handler:backProjectDetail
	            }]
	      }]
	});
	
	var viewport = Ext.create('Ext.Viewport', {
		layout : 'border',
		items : [form]
	});
	
	function changeBill(){
		//验证数据
		var flag = true;
		form.items.each(function(item,index,length){
			if(item.getValue()==null||item.getValue()==""){
				if(item.initialConfig.id != "billRemark"){
					Ext.Msg.alert("提示", item.initialConfig.fieldLabel+"不能为空！");
					flag = false;
                     return;
				}

			}
		});

		if(flag) {
            Ext.Ajax.request({
                url: jypath + '/marketing/haveReceiveRefund/edit',
                params: {
                    newBillCode: Ext.getCmp("newBillCode").getValue(),
                    billUserId: Ext.getCmp("billUserId").getValue(),
                    date: Ext.getCmp("billDate").getValue(),
                    billRemark: Ext.getCmp("billRemark").getValue(),
                    id: billId
                },
                callback: function (options, success, response) {
                    if (success) {
                        Ext.MessageBox.alert("系统提示", "更换成功");
                        setTimeout(function () {
                            backProjectDetail();
                        }, 2000);
                    } else {
                        Ext.MessageBox.alert("系统提示", "系统异常!");
                    }
                }

            });
        }
	}
	
	function backProjectDetail(){
		location.href=jypath+"/marketing/needReceiveRefund/index";
	}
	
});